#!/usr/bin/python

import yaml,sys,getopt

input_file=""
yaml_file=""

def usage():
    print("Usage:",sys.argv[0],"-s ymal_file -i input_file")


try:
    opts,args = getopt.getopt(sys.argv[1:],"i:y:")
except getopt.GetoptError:
        usage()
        exit(1)
    
for op,value in opts:
    if op=="-i":
        input_file = value
    elif op=='-y':
        yaml_file = value
    else:
        usage()
        exit()
     


if not input_file or not yaml_file:
    usage()
    exit(1)

file_list=[]
match=0
with open(input_file) as fi:
    line=fi.readline()
    while line:
        name=line.split()[0]
        repo=line.split()[1]
        file_list.append(tuple([name,repo]))
        line=fi.readline()

file_dict=dict(file_list)

with open(yaml_file) as f:
    data=yaml.load(f)

for group in data['sigs']:
    for key1,value1 in file_dict.items():
        for key,value in group.items():
            if key=="name" and value==key1:
                match=1 
            elif key=="repositories" and match==1:
                value.append(value1)
                value.sort()
                #print(value)
                match=0


with open("result.yaml",'w') as yf:
    yaml.dump(data,yf)

print("Succeed,Result are stored in result.yaml!")
